package com.css.fxfzysx.modules.ysxManagement.repository.impl;

import com.css.fxfzysx.modules.ysxManagement.entity.DdRatioEntity;
import com.css.fxfzysx.modules.ysxManagement.entity.RsRatioEntity;
import com.css.fxfzysx.modules.ysxManagement.entity.WsfCoefficientEntity;
import com.css.fxfzysx.modules.ysxManagement.repository.WsfCoefficientRepository;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository
public class WsfCoefficientRepositoryImpl implements WsfCoefficientRepository {

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Resource
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public void saveWsfCoefficient(WsfCoefficientEntity it) {
        jdbcTemplate.update("insert into czt_water_damage_forecast(cwdf_id,level,description,width,create_user,create_time,del_flag,project_id,default_data) " +
                "values(?,?,?,?,?,?,?,?,?)", it.getCwdfId(), it.getLevel(), it.getDescription(), it.getWidth(),it.getCreateUser(), it.getCreateTime(), it.getDelFlag(), it.getProjectId(),it.getDefaultData());
    }

    @Override
    public void updateWsfCoefficient(WsfCoefficientEntity it) {
        String sql = "update czt_water_damage_forecast set level= :level,description= :description,width= :width,update_user= :updateUser,update_time= :updateTime,project_id= :projectId" +
                " where cwdf_id= '"+it.getCwdfId()+"'";
        SqlParameterSource source = new BeanPropertySqlParameterSource(it);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public List<WsfCoefficientEntity> getAllWsfCoefficient() {
        String sql = "select * from czt_water_damage_forecast where del_flag='0' order by create_time asc";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(WsfCoefficientEntity.class));
    }

    @Override
    public void deleteWsfCoefficientById(String wsfCoefficientId) {
        String sql = "update czt_water_damage_forecast set del_flag = '1' where cwdf_id=?";
        jdbcTemplate.update(sql, wsfCoefficientId);
    }

    @Override
    public void saveRsRatio(RsRatioEntity it) {
        jdbcTemplate.update("insert into czt_rebar_shape_ratio(crsr_id,rebar_type,rebar_value,create_user,create_time,del_flag,project_id,default_data) " +
                "values(?,?,?,?,?,?,?,?)", it.getCrsrId(), it.getRebarType(), it.getRebarValue(),it.getCreateUser(), it.getCreateTime(), it.getDelFlag(), it.getProjectId(),it.getDefaultData());
    }

    @Override
    public void updateRsRatio(RsRatioEntity it) {
        String sql = "update czt_rebar_shape_ratio set rebar_type= :rebarType,rebar_value= :rebarValue,update_user= :updateUser,update_time= :updateTime,project_id= :projectId" +
                " where crsr_id= '"+it.getCrsrId()+"'";
        SqlParameterSource source = new BeanPropertySqlParameterSource(it);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public List<RsRatioEntity> getAllRsRatio() {
        String sql = "select * from czt_rebar_shape_ratio where del_flag='0' order by create_time asc";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(RsRatioEntity.class));
    }

    @Override
    public void deleteRsRatioById(String rsRatioId) {
        String sql = "update czt_rebar_shape_ratio set del_flag = '1' where crsr_id=?";
        jdbcTemplate.update(sql, rsRatioId);
    }

    @Override
    public void deleteWsfCoefficientByProjectId(String arrangeId) {
        String sql = "update czt_water_damage_forecast set del_flag = '1' where project_id=?";
        jdbcTemplate.update(sql, arrangeId);
    }

    @Override
    public void deleteRsRatioByProjectId(String arrangeId) {
        String sql = "update czt_rebar_shape_ratio set del_flag = '1' where project_id=?";
        jdbcTemplate.update(sql, arrangeId);
    }

    @Override
    public List<RsRatioEntity> getRsRatioById(String crsrId) {
        String sql = "select * from czt_rebar_shape_ratio where crsr_id='"+crsrId+"'";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(RsRatioEntity.class));
    }
}
